import Mock from 'mockjs';
import zutil from '@/util/util'
import {base_med_treeS} from '@/util/baseData';

Mock.setup({
  timeout: '50'
})

const w_sex = ['男', '女'];
const w_times = ['凌晨', '早餐前', '早餐后', '午餐前', '午餐后', '晚餐前', '晚餐后', '睡前'];
const w_timesD = ['凌晨', '早餐前', '早餐后', '午餐前', '午餐后', '晚餐前', '晚餐后', '睡前', '随机'];
// 糖尿病类型
const w_cgmType = ['Ⅰ型糖尿病', 'Ⅱ型糖尿病', '特殊糖尿病', '妊娠糖尿病'];
// 系统角色类型
const w_roleType = [{id: 1, name: '医生'}, {id: 2, name: '主任'}, {id: 3, name: '护士长'}, {id: 4, name: '管理员'}];
// 医生职称类型
const w_doctorType = ['住院医师', '主治医师', '副主任医师', '主任医师'];

const zrangeDate = function(date, inter=1) {
	return new Date(new Date(date).getTime() + 3600 * 1000 * 24 * inter).formatDD("yyyy-MM-DD");
}

// 获取 mock.Random 对象
const Random = Mock.Random;
// mock一组数据
const test = function() {
  let articles = [];
  for (let i = 0; i < 10; i++) {
    let newArticleObject = {
      title: Random.csentence(5, 30), //  Random.csentence( min, max )
      thumbnail_pic_s: Random.dataImage('300x250', 'mock的图片'), // Random.dataImage( size, text ) 生成一段随机的 Base64 图片编码
      author_name: Random.cname(), // Random.cname() 随机生成一个常见的中文姓名
      date: Random.date() + ' ' + Random.time() // Random.date()指示生成的日期字符串的格式,默认为yyyy-MM-dd；Random.time() 返回一个随机的时间字符串
    }
    articles.push(newArticleObject)
  }

  return {
		stat: 0,
		articles: articles
  }
}

const zlogin = function(param) {
	return {
		stat: 0,
		msg: '',
		data: {
			token: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1NzAxMjkyMDAsImlhdCI6MTU2OTQ4NjI3MiwiZGF0YSI6eyJpZCI6MjIsInR5cGUiOjIsInVzZXJuYW1lIjoiZ3VhbmxpeXVhbiIsInJlYWxuYW1lIjoiXHU3YmExXHU3NDA2XHU1NDU4MSIsImRlcGFydG1lbnQiOm51bGwsImdyb3VwIjpudWxsLCJyb2xlIjp7ImlkIjo0LCJuYW1lIjoiXHU3YmExXHU3NDA2XHU1NDU4In19fQ.1ZaQRvvXLI-Rm3g4drJh_fo1YNQfRPX7-gg43QKPGSA'
		}
	};
}

const zlogOut = function(param) {
	return {
		stat: 0,
		msg: '',
		data: {}
	};
}

const zloginInfo = function(param) {
	let attr = {
		id: Random.id(),
		name: Random.cname(),
		account: Random.word(5, 10),
		phone: Mock.mock(/^1[385][1-9]\d{8}/),
		icon: Random.dataImage('40x40', 'icon'),
		keshi: Random.cword(2, 5),
		// roleType: w_roleType[3]
		roleType: w_roleType[Random.integer(0, w_roleType.length-1)]
	}
	return {
		stat: 0,
		msg: '',
		data: attr
	};
}

const zmedList = function(param) {
	let arr = [];
	for (let i = 0; i < 10; i++) {
		let _rand = Random.integer(0, 3),
			_p1 = base_med_treeS[_rand],
			_len = _p1.children.length,
			_rand2 = Random.integer(0, base_med_treeS[_rand].children.length-1),
			_p2 = _p1.children[_rand2];
		let attr = {
			id: Random.increment(1000),
			name: Random.cword(2, 5),
			category_id: _p1.id,
			category: _p1.name,
			medcategory_id: _p2.id,
			category_name: _p2.name,
			factory: Random.cword(2, 5),
			specifications: Random.float(0.1, 1).toFixed(1)+'mg X '+Random.integer(5, 10).toFixed(1)+'ml',
			instructions: Random.cword(20, 200)
		}
		arr.push(attr)
	}

	return {
		stat: 0,
		data: arr,
		total: Random.integer(12, 66)
	};
}

const zmedSave = function(param) {
	return {
		stat: 0,
		msg: '',
		data: {}
	};
}

const zmedMod = function(param) {
	return {
		stat: 0,
		msg: '',
		data: {}
	};
}

const zmedDel = function(param) {
	return {
		stat: 0,
		msg: '',
		data: {}
	};
}

const zmedById = function(param) {
	return {
		stat: 0,
		msg: '',
		data: {}
	};
}

const zPurchaseList = function(param) {
	let arr = [];
	const len = Random.integer(0, 10);
	for (let i = 0; i < 5; i++) {
		let attr = {
			id: Random.id(),
			purchasecode: Random.string('number', 5),
			medcode: Random.string('number', 5),
			name: Random.cword(2, 5),
			specification: Random.float(0.1, 1).toFixed(1)+'mg X '+Random.integer(5, 10).toFixed(1)+'ml',
			unit: '盒',
			factory: Random.cword(2, 5),
			approvalno: Random.string('number', 5),
			dosagetype: '剂',
			purchaseNum: Random.integer(200, 1000),
			unitprice: Random.float(10, 100).toFixed(2),
			price: Random.float(100, 1000).toFixed(2),
			stocknum: Random.integer(200, 1000),
			salenum: Random.integer(200, 1000),
			stockstatus: Random.integer(1, 2),
			status: Random.integer(1, 2),
			remark: Random.boolean() ? Random.csentence() : ''
		}
		arr.push(attr)
	}
	
	return {
		stat: 0,
		data: arr,
		total: Random.integer(0, 20)
	}
}

const zInquiryList = function(param) {
	let arr = [];
	const len = Random.integer(0, 10);
	for (let i = 0; i < 10; i++) {
		let attr = {
			id: Random.id(),
			inquirycode: Random.string('number', 5),
			medcode: Random.string('number', 5),
			name: Random.cword(2, 5),
			specification: Random.float(0.1, 1).toFixed(1)+'mg X '+Random.integer(5, 10).toFixed(1)+'ml',
			unit: '盒',
			factory: Random.cword(2, 5),
			approvalno: Random.string('number', 5),
			dosagetype: '剂',
			num: Random.integer(200, 1000),
			price: Random.float(100, 1000).toFixed(2),
			status: Random.integer(1, 2),
			remark: Random.boolean() ? Random.csentence() : ''
		}
		arr.push(attr)
	}
	
	return {
		stat: 0,
		data: arr,
		total: Random.integer(0, 20)
	}
}

const zpatientList = function(param) {
	let arr = [];
	const len = Random.integer(0, 10);
	for (let i = 0; i < 10; i++) {
		let attr = {
			id: Random.id(),
			icon: Random.boolean() ? Random.dataImage('100x100', '头像') : '',
			phone: Mock.mock(/^1[385][1-9]\d{8}/),
			realname: Random.cname(),
			gender: Random.integer(1, 2),
			birthday: Random.date(),
			diabetes_type: Random.integer(1, 3),
			bed_num: Random.integer(1, 100),
			nursing_level: Random.integer(1, 3),
			doctor: {
				id: Random.id(100),
				username: Random.string(5),
				realname: Random.cname(),
				role: Random.cword(2, 5),
				detp_group: Random.cword(2, 5),
			},
			modifiable: 1,
			cgm_value: Random.float(1, 33).toFixed(1),
			fg_checked_plan: [null, null],
			finger_value: [null, null]
		}
		arr.push(attr)
	}
	
	return {
		stat: 0,
		data: arr,
		total: Random.integer(12, 66)
	}
}

const zchangeHeadIcon = function(param) {
	console.log(param);
	return {
		stat: 0,
		msg: '',
		data: {}
	};
}


// Mock.mock( url, post/get , 返回的数据);
/**
 * 带参数的get方法只能使用正则去匹配
 * /api/zsingle/10001?channel=1&check_date=1564984632167
**/

Mock.mock('/api/zlogin', 'post', zlogin);
Mock.mock('/api/zlogOut', 'post', zlogOut);
Mock.mock('/api/zloginInfo', 'post', zloginInfo);
Mock.mock(RegExp('/api/zmedList' + ".*"), 'get', zmedList);
Mock.mock(RegExp('/api/zmedById' + ".*"), 'get', zmedById);
Mock.mock('/api/zmedSave', 'post', zmedSave);
Mock.mock('/api/zmedMod', 'put', zmedMod);
Mock.mock(RegExp('/api/zmedDel' + ".*"), 'delete', zmedDel);
Mock.mock(RegExp('/api/zPurchaseList' + ".*"), 'get', zPurchaseList);
Mock.mock(RegExp('/api/zInquiryList' + ".*"), 'get', zInquiryList);

Mock.mock(RegExp('/api/zpatientList' + ".*"), 'get', zpatientList);
Mock.mock('/api/zchangeHeadIcon', 'post', zchangeHeadIcon);

export default Mock;
